﻿#include "business.h"
using namespace std;
using namespace uv;

#define FRA_CMD_INFO         			0x0000   //通知信息
#define FRA_CMD_CONNECT         		CMD_REGISTER_APP   //连接请求
#define FRA_CMD_CONNECT_R         	0x8001   //连接应答
#define FRA_CMD_MSG_SEND         	0x0002   //短信发送请求
#define FRA_CMD_MSG_SEND_R         	0x8002   //短信发送应答
#define FRA_CMD_MSG_RECV         	0x0003   //短信接收请求
#define FRA_CMD_MSG_RECV_R         	0x8003   //短信接收应答
#define FRA_CMD_PHONE_OUT         	0x0004   //语音拨打请求
#define FRA_CMD_PHONE_OUT_R        0x8004   //语音拨打应答
#define FRA_CMD_PHONE_IN         		0x0005   //语音接听请求
#define FRA_CMD_PHONE_IN_R         	0x8005   //语音接听应答
#define FRA_CMD_QUERY         			0x0006   //终端查询请求
#define FRA_CMD_QUERY_R         		0x8006   //终端查询应答


#define FRA_CONNECT_R_SUCCESS 			0x00   //连接并认证成功
#define FRA_CONNECT_R_NOUSER				0x01   //无该用户
#define FRA_CONNECT_R_WRONGPW			0x02   //密码错误
#define FRA_CONNECT_R_ONLINEALREADY	0x03   //该用户已经在线登录
#define FRA_CONNECT_R_END_SUCCESS		0x04   //断开连接成功
#define FRA_CONNECT_R_REG_SUCCESS		0x05   //注册成功
#define FRA_CONNECT_R_REG_USEREXIST	0x06   //该用户名已被注册
#define FRA_CONNECT_R_FORGOT_SUCCESS	0x07   //密码重置成功
#define FRA_CONNECT_R_FORGOT_FAIL		0x08   //密码重置失败
#define FRA_CONNECT_R_ERROR_SERVER	0x09   //服务器无响应
////////////////////////implement function//////////////////////////

void OnMsgSendRequest(const NetPacket* packet, const BusinessPacket * business_packet, const unsigned char* buf, const int datalen, TCPClient* client)
{
    
}

void OnAuthSign(const NetPacket* packet, const BusinessPacket * business_packet, const unsigned char* buf, const int datalen, TCPClient* client)
{

    //LOGD( "action %d, %s", packet.action, senddata);
	
    #pragma pack(1)//将当前字节对齐值设为1
    typedef struct _RegisterApp {
        unsigned char type;
        unsigned char username[20];
        unsigned char password[20];
    }RegisterApp;
    #pragma pack()//将当前字节对齐值设为默认值(通常是4)
    
    ASSERT(datalen == sizeof(RegisterApp),"wrong packege length");
    RegisterApp *register_app = (RegisterApp *)buf;
    register_app->username[19]=0;
    register_app->password[19]=0;
    LOGD("type:0x%x, username: %s, password: %s", register_app->type, register_app->username, register_app->password);
    
    NetPacket packet_reply = *packet;
    packet_reply.cmd = FRA_CMD_CONNECT_R;
    unsigned char userauth_business_reply_code = FRA_CONNECT_R_SUCCESS;

    StreamBuffer sb = PacketDataWithBusiness(packet_reply,*business_packet,&userauth_business_reply_code,sizeof(userauth_business_reply_code));
    if (client->Send(sb) <= 0) {
        fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
        return;
    }
    LOGD("send auth reply");
    return;
}

struct CMD_Handler handler[] =
{
    {CMD_REGISTER_APP,OnAuthSign},
    {FRA_CMD_MSG_SEND,OnMsgSendRequest}
};

void OnPacket(const NetPacket& packet, const unsigned char* buf, TCPClient* client)
{
	for(int i=0; i<sizeof(handler) / sizeof(struct CMD_Handler); i++)
    {
        if (packet.cmd == handler[i].cmd)
        {
            ASSERT(packet.datalen >= sizeof(BusinessPacket),"BusinessPacket is not found");
            BusinessPacket *business_packet = (BusinessPacket*)buf;
            LOGD("cmd:0x%x, client_id:%d, datalen:%d", packet.cmd, business_packet->request_id, packet.datalen-sizeof(BusinessPacket));
            handler[i].func(&packet,business_packet, buf+sizeof(BusinessPacket), packet.datalen-sizeof(BusinessPacket), client);
            break;
        }
    }
}

////////////////////////register function///////////////////////////
ClientType init_business(struct CMD_Handler ** _handler, int *count)
{
	(*_handler) = handler;
    (*count) = sizeof(handler) / sizeof(struct CMD_Handler);
	return CLIENT_TYPE_BUSINESS_USER_AUTH;
}
